<?php
namespace app\admin\controller;
use app\admin\validate\LoginValidate;
use app\admin\model\Admin;
use think\exception\ValidateException;
use think\facade\View;
use think\captcha\facade\Captcha;
use app\BaseController;


class LoginController extends BaseController
{

    public function index(Admin $admin)
    {
        if($this->request->isGet()){
            return View::fetch();
        }
        try {
            $data = $this->request->param();
            validate(LoginValidate::class)->check($data);
            $result = $admin->login($data['username'],$data['password']);
            if(empty($result)){
                return $this->returnJson('账号或者密码错误');
            }
            return $this->returnJson(SUCCESS_MESSAGE);
        } catch (ValidateException $e) {
            return $this->returnJson($e->getError());
        }
    }

    public function captcha()
    {
        return Captcha::create();
    }
}